<template>
  <div>
    <!-- 面包屑导航区域 -->
    <el-breadcrumb separator-class="el-icon-arrow-right">
      <el-breadcrumb-item>基础设置</el-breadcrumb-item>
      <el-breadcrumb-item>轮播图管理</el-breadcrumb-item>
    </el-breadcrumb>

    <!-- 卡片视图区域 -->
    <el-card>
      <!-- 搜索与添加区域 -->
      <el-row :gutter="20">
        <el-col :span="5">
          <el-button type="primary" @click="toAddCarousel"
            >添加轮播图</el-button
          >
        </el-col>
      </el-row>

      <!-- 用户列表区域 -->
      <el-table :data="CarouselList" border stripe>
        <!-- <el-table-column type="index"></el-table-column> -->
        <el-table-column label="图片名称" prop="name"></el-table-column>
        <el-table-column label="图片" prop="image">
          <template slot-scope="scope">
            <img
              :src="$global.imgURL+scope.row.image"
              alt=""
              style="width: 155px; height: 100px"
            />
          </template>
        </el-table-column>
        <el-table-column label="类型" prop="type"></el-table-column>
        <el-table-column label="操作" width="180px">
          <template slot-scope="scope">
            <!-- 修改按钮 -->
            <el-button
              type="primary"
              icon="el-icon-edit"
              size="mini"
              @click="toUpCarousel(scope.row.id)"
            ></el-button>
            <!-- 删除按钮 -->
            <el-button
              type="danger"
              icon="el-icon-delete"
              size="mini"
              @click="removeCarouselById(scope.row.id)"
            ></el-button>
          </template>
        </el-table-column>
      </el-table>
    </el-card>
  </div>
</template>

<script>
export default {
  data() {
    return {
      CarouselList: [],
    };
  },
  created() {
    //获取轮播图列表
    this.getCarousel();
  },
  methods: {
    async getCarousel() {
      const { data: res } = await this.$http.get("carousel/listCarousel");
      console.log(res);
      if (res.code !== 200) {
        return this.$message.error("获取轮播列表失败！");
      }
      this.CarouselList = res.data.carouselPOS;
      console.log(res);
    },
    toAddCarousel() {
      this.$router.replace("/carousel/addCarousel");
    },
    async toUpCarousel(id) {
      console.log(id);
      this.$router.push("/carousel/upCarousel/" + id);
    },
    // 根据Id删除对应的用户信息
    async removeCarouselById(id) {
      console.log(id);
      // 弹框询问用户是否删除数据
      const confirmResult = await this.$confirm(
        "此操作将永久删除该用户, 是否继续?",
        "提示",
        {
          confirmButtonText: "确定",
          cancelButtonText: "取消",
          type: "warning",
        }
      ).catch((err) => err);

      // 如果用户确认删除，则返回值为字符串 confirm
      // 如果用户取消了删除，则返回值为字符串 cancel
      // console.log(confirmResult)
      if (confirmResult !== "confirm") {
        return this.$message.info("已取消删除");
      }

      let params = new FormData();
      params.append("id", id);
      //调用删除接口
      const { data: res } = await this.$http.post("carousel/deleteCarousel", params);

      if (res.code !== 200) {
        this.$message.error("删除轮播图失败！");
      }else{
        this.$message.success("删除轮播图成功！");
      }
      this.getCarousel();
    },
  },
};
</script>

<style lang="less" scoped>
.button:hover {
  background-color: rgb(64, 158, 255);
  color: rgb(255, 255, 255);
}
</style>
